package cn.wolfcode.luowowo.acticle.service.impl;


import cn.wolfcode.luowowo.acticle.domain.Destination;
import cn.wolfcode.luowowo.acticle.domain.Travel;
import cn.wolfcode.luowowo.acticle.domain.TravelContent;
import cn.wolfcode.luowowo.acticle.mapper.TravelContentMapper;
import cn.wolfcode.luowowo.acticle.mapper.TravelMapper;
import cn.wolfcode.luowowo.acticle.mapper.TravelTagMapper;
import cn.wolfcode.luowowo.acticle.query.TravelQuery;
import cn.wolfcode.luowowo.acticle.service.IDestinationService;
import cn.wolfcode.luowowo.acticle.service.ITravelService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;

//@Component
@Service
@Transactional
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelMapper travelMapper;

    @Autowired
    private TravelContentMapper travelContentMapper;

    @Autowired
    private TravelTagMapper travelTagMapper;

    @Autowired
    private IDestinationService destinationService;





    @Override
    public PageInfo query(TravelQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());

        return new PageInfo(travelMapper.selectForList(qo));
    }

    @Override
    public void changeState(Travel travel) {
        //如果是发布状态，设置
        if(travel.getState() == Travel.STATE_RELEASE){
            travel.setReleaseTime(new Date());
        }
        travelMapper.updateTravelState(travel);
    }

    @Override
    public Travel get(Long id) {
        Travel travel = travelMapper.selectByPrimaryKey(id);
        TravelContent travelContent = travelContentMapper.selectByPrimaryKey(id);
        travel.setTravelContent(travelContent);
        return travel;
    }

    @Override
    public List<Travel> queryTravelByDestId(Long destId) {


        TravelQuery qo = new TravelQuery();
        qo.setDestId(destId);
        PageHelper.startPage(qo.getCurrentPage(), 3, "t.viewnum desc");
        return travelMapper.selectForList(qo);
    }

    @Override
    public Long saveOrUpdate(Travel travel) {
        //作者id

        //游记摘要
        if(travel.getTravelContent().getContent().length() < 200){
            travel.setSummary(travel.getTravelContent().getContent());
        }else{
            String summary = travel.getTravelContent().getContent().substring(0, 200);
            travel.setSummary(summary);
        }

        travel.setLastUpdateTime(new Date());

        //保存
        if(travel.getId() == null){
            travel.setCreateTime(new Date());
            travelMapper.insert(travel);
            TravelContent content = travel.getTravelContent();
            content.setId(travel.getId());
            travelContentMapper.insert(content);
        }else{
            travelTagMapper.deleteRelation(travel.getId());

            travelMapper.updateByPrimaryKey(travel);
            TravelContent content = travel.getTravelContent();
            content.setId(travel.getId());
            travelContentMapper.updateByPrimaryKey(content);

        }

        return travel.getId();
    }

    @Override
    public List<Travel> list() {
        return travelMapper.selectAll();
    }


    @Override
    public PageInfo<Travel> queryMyTravel2(TravelQuery qo, UserInfo userInfo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Travel> list = travelMapper.selectByUserId(userInfo.getId());
        //判断每一个目的地是否有父类目的地
        //有则加入属性parentDest,没有则不加
        for (Travel travel : list) {
            List<Destination> toast = destinationService.getToastById(travel.getDest().getId());
            //如果长度大于1代表有父类目的地
            if (toast.size() > 1) {
                toast.remove(toast.size() -1);
                Collections.reverse(toast);
                travel.setParentDest(toast);
            }
        }
        return new PageInfo<>(list);
    }




}
